package wms

import (
	"context"
	"encoding/json"
	"ez/apps/wms/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type ItemBase struct {
	mgo.BaseDoc `bson:"-" json:"-"`
	Id          int64            `json:"id" bson:"id,omitempty"`
	Name        string           `json:"name" bson:"name"`             //名称
	Category    *ItemCategory    `json:"category" bson:"category"`     //分类
	CategoryId  int64            `json:"categoryId" bson:"categoryId"` //分类
	Unit        *Unit            `json:"unit" bson:"unit"`             //单位
	UnitId      int64            `json:"unitId" bson:"unitId"`         //单位
	Attributes  []*ItemAttribute `json:"attributes" bson:"attributes"` //属性
	Skus        []*ItemSku       `json:"skus" bson:"skus"`             //SKU
	Version     int64            `json:"version" bson:"version"`       //版本
	CreateAt    time.Time        `json:"createAt" bson:"createAt"`
	UpdateAt    time.Time        `json:"updateAt" bson:"updateAt"`
	DeleteAt    *time.Time       `json:"deleteAt" bson:"deleteAt"`
}

func (this *ItemBase) DocName() string { return "ItemBase" }
func (this *ItemBase) GetId() int64    { return this.Id }
func (this *ItemBase) SetId(id int64)  { this.Id = id }
func (this *ItemBase) Create() error {
	return this.GetFactory().Create(this)
}
func (this *ItemBase) Replace() error {
	return this.GetFactory().Replace(this)
}
func (this *ItemBase) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *ItemBase) Delete() error {
	return this.GetFactory().Delete(this)
}
func (this *ItemBase) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *ItemBase) Destroy() error {
	return this.GetFactory().Destroy(this)
}
func (this *ItemBase) ToString() string {
	return string(this.ToBytes())
}
func (this *ItemBase) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *ItemBase) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *ItemBase) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}
func (this *ItemBase) LoadCategory() {
	if this.CategoryId == 0 {
		return
	}
	this.Category, _ = NewItemCategoryCrud().FindId(this.CategoryId)
}
func (this *ItemBase) LoadUnit() {
	if this.UnitId == 0 {
		return
	}
	this.Unit, _ = NewUnitCrud().FindId(this.UnitId)
}
func (this *ItemBase) LoadAttributes() {
	attributes, _ := NewItemAttributeCrud().FindBy(ss.M{"attributesIds": this.Id}, []string{"id"}, 0, 0)
	this.Attributes = attributes
}
func (this *ItemBase) LoadSkus() {
	skus, _ := NewItemSkuCrud().FindBy(ss.M{"skusIds": this.Id}, []string{"id"}, 0, 0)
	this.Skus = skus
}
func (this *ItemBase) ClearRelationsBeforeSave() mgo.Doc {
	this.Category = nil
	this.Unit = nil
	this.Attributes = nil
	this.Skus = nil
	return this
}
func neverUsed_ItemBase() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type ItemBaseAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *ItemBase
	Session *ez.Session
}

func NewItemBaseAccessControl(model *ItemBase, action string, session *ez.Session) *ItemBaseAccessControl {
	ctrl := &ItemBaseAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.ItemBaseAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
